<?php
/**
 * Created by PhpStorm.
 * User: zjw
 * Date: 2018/4/25
 * Time: 下午3:47
 */

namespace api\modules\v1\controllers;

use common\models\CouponCategory;
use common\models\Coupon;
use common\models\UserCoupon;
use common\models\Groupbuy;
use common\models\Product;
use common\service\ServiceFactory;
use yii;

class GroupbuyController extends RestController
{
    public $modelClass = 'common\models\Groupbuy';


    public function actionStartGroup()
    {
        $product_id=Yii::$app->request->post("product_id");
        $activity_id=Yii::$app->request->post("activity_id");
        try {
            $data = ServiceFactory::getInstance()->getGroupbuyService()->startGroup($product_id,$activity_id);
            return $data;


        } catch (\Exception $e) {
            return ['code' => -99, 'message' => $e->getMessage()];
        }

    }
    public function actionJoinGroup()
    {
        $group_id=Yii::$app->request->post("group_id");
        try {
            $data = ServiceFactory::getInstance()->getGroupbuyService()->joinGroup($group_id);
            return $data;


        } catch (\Exception $e) {
            return ['code' => -99, 'message' => $e->getMessage()];
        }

    }
    public function actionGetGroup(){


        //根据团id找到 商品id  找到优惠券分类id  找到优惠券id
        $group_id=Yii::$app->request->post("group_id");
        if(empty($group_id)){
            return ['code'=>-1,'message'=>'团不存在'];
        }
        $groupbuy = Groupbuy::findOne($group_id);
        if(empty($groupbuy)){
            return ['code'=>-1,'message'=>'团不存在'];
        }
        $product_id=$groupbuy->goodsid;
        $product = Product::findOne($product_id);
        if(empty($product)){
            return ['code'=>-2,'message'=>'商品不存在'];
        }
        $couponCatagory_id = $product->coupon_id;
        $couponCatagory = CouponCategory::getCollection()->findOne(['id'=>$couponCatagory_id]);
        if(empty($couponCatagory)){
            return ['code'=>-3,'message'=>'优惠券分类不存在'];
        }
        $mark_id=$couponCatagory['mark_id'];
        $image=$couponCatagory['image'];
        $coupon = Coupon::getCollection()->findOne(['catalog_mark_id'=>$mark_id]);
        $coupon['image']=$image;
        $coupon['desc']=json_decode($coupon['desc']);
        $coupon_id=$coupon['id'];
        //根据用户，找到领过的所有券，
        $user_id=Yii::$app->user->id;
        $database = Yii::$app->mongo->getDatabase();
        $user_coupon_collection = $database->getCollection("user_coupon");
        $user_coupons = $user_coupon_collection->find(['user_id' => intval($user_id)])->toArray();
        if(count($user_coupons)==0){
            return ['code'=>0,'message'=>'本用户没有领过'];
        }
        $ids=[];
        foreach($user_coupons as $user_coupon){
            $ids[]=$user_coupon['coupon_id'];
        }
        //看这个团对应的优惠券在不在用户领过的里头，有就返回
        if(in_array($coupon_id,$ids)){

            return ['code'=>1,'message'=>'success','data'=>$coupon];
        }else{
            return ['code'=>0,'message'=>'本用户没有领过'];
        }

    }

}
